<template>
  <el-container class="comment-view panel-block">
    <el-header class="sa-header">
      <div class="sa-title sa-flex sa-row-between">
        <div class="label sa-flex">
          <span class="left">评价列表</span>
          <search-condition
            :conditionLabel="filterParams.conditionLabel"
            @deleteFilter="deleteFilter"
          ></search-condition>
        </div>
        <div>
          <el-button class="sa-button-refresh" icon="RefreshRight" @click="getData()"></el-button>
          <el-button
            v-if="!modal"
            class="sa-button-refresh"
            icon="Search"
            @click="openFilter"
          ></el-button>
          <el-button v-auth="'shop.admin.goods.comment.add'" @click="fakeCommentRow" plain
            >添加虚拟评价</el-button
          >
          <el-button
            v-auth="'shop.admin.goods.comment.recyclebin'"
            type="danger"
            icon="Delete"
            plain
            @click="openRecyclebin"
            >回收站</el-button
          >
        </div>
      </div>
    </el-header>
    <el-main class="sa-p-0">
      <div class="sa-table-wrap panel-block panel-block--bottom" v-loading="loading">
        <el-table
          height="100%"
          class="sa-table"
          :data="table.data"
          @selection-change="changeSelection"
          @sort-change="fieldFilter"
          @row-dblclick="editRow"
          row-key="id"
          stripe
        >
          <template #empty>
            <sa-empty />
          </template>
          <el-table-column type="selection" width="48" align="center"></el-table-column>
          <el-table-column label="商品信息" min-width="310">
            <template #default="scope">
              <div class="sa-flex" v-if="scope.row.order_item">
                <div class="sa-m-r-8">
                  <sa-preview :url="scope.row.order_item.goods_image" size="40"></sa-preview>
                </div>
                <div>
                  <span
                    class="sa-table-line-1 goods-title cursor-pointer"
                    @click="openGoods(scope.row.order_item.goods_id)"
                  >
                    {{ scope.row.order_item.goods_title || '-' }}
                  </span>
                  <span class="sa-table-line-1" v-if="scope.row.order">
                    订单号：{{ scope.row.order.order_sn }}
                    <sa-svg
                      class="copy sa-m-l-4 cursor-pointer"
                      name="sa-copy"
                      @click="useClip(scope.row.order.order_sn)"
                    ></sa-svg>
                  </span>
                  <span v-else>订单号：-</span>
                </div>
              </div>
              <div class="sa-flex" v-else-if="scope.row.goods">
                <div class="sa-m-r-8">
                  <sa-preview :url="scope.row.goods.image" size="40"></sa-preview>
                </div>
                <div>
                  <span
                    class="sa-table-line-1 goods-title cursor-pointer"
                    @click="openGoods(scope.row.goods.id)"
                  >
                    {{ scope.row.goods.title || '-' }}
                  </span>
                  <span>虚拟评价</span>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="用户信息" min-width="96" align="center">
            <template #default="scope">
              <sa-user-profile
                :user="scope.row.user"
                :id="scope.row.user_id"
                mode="col"
                :isHover="scope.row.user_type != 'fake_user'"
              />
            </template>
          </el-table-column>
          <el-table-column label="评价星级" min-width="146" align="center">
            <template #default="scope">
              <el-rate v-model="scope.row.level" disabled />
            </template>
          </el-table-column>
          <el-table-column label="评价图片" width="200" align="center">
            <template #default="scope">
              <el-scrollbar class="images-wrap">
                <div v-if="scope.row.images" class="sa-flex">
                  <div v-for="i in scope.row.images" :key="i" class="sa-m-r-8">
                    <sa-preview :url="i" size="30"></sa-preview>
                  </div>
                </div>
              </el-scrollbar>
            </template>
          </el-table-column>
          <el-table-column label="评价内容" min-width="162" align="center">
            <template #default="scope">
              <div class="sa-table-line-1">
                {{ scope.row.content || '-' }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="评价时间" min-width="180" align="center">
            <template #default="scope">
              {{ scope.row.create_time || '-' }}
            </template>
          </el-table-column>
          <el-table-column label="显示状态" min-width="130" align="center">
            <template #default="scope">
              <el-dropdown trigger="click" @command="handleCommand">
                <el-button
                  v-auth="'shop.admin.goods.comment.edit'"
                  class="status-btn"
                  :class="scope.row.status == 'normal' ? 'success-btn' : ''"
                >
                  {{ scope.row.status_text }}
                  <el-icon class="el-icon--right"><ArrowDown /></el-icon>
                </el-button>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item
                      :command="{
                        id: scope.row.id,
                        type: 'normal',
                      }"
                    >
                      <span class="status-normal">正常</span>
                    </el-dropdown-item>
                    <el-dropdown-item
                      :command="{
                        id: scope.row.id,
                        type: 'hidden',
                      }"
                      ><span class="status-hidden">隐藏</span></el-dropdown-item
                    >
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" min-width="120">
            <template #default="scope">
              <el-button
                v-auth="'shop.admin.goods.comment.detail'"
                type="primary"
                class="is-link"
                @click="editRow(scope.row)"
                >编辑</el-button
              >
              <el-popconfirm
                width="fit-content"
                confirm-button-text="确认"
                cancel-button-text="取消"
                title="确认删除这条记录?"
                @confirm="deleteApi(scope.row.id)"
              >
                <template #reference>
                  <el-button
                    v-auth="'shop.admin.goods.comment.delete'"
                    type="danger"
                    class="is-link"
                  >
                    删除
                  </el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-main>
    <sa-view-bar>
      <template #left>
        <sa-batch-handle
          :batchHandleTools="batchHandleTools"
          :selectedLeng="table.selected.length"
          @batchHandle="batchHandle"
        ></sa-batch-handle>
      </template>
      <template #right>
        <sa-pagination :pageData="pageData" @updateFn="getData" />
      </template>
    </sa-view-bar>
  </el-container>
</template>
<script setup>
  import { onMounted, reactive, ref } from 'vue';
  import { api } from '../goods.service';
  import { ElMessageBox } from 'element-plus';
  import { useModal, useSearch, usePagination } from '@/sheep/hooks';
  import { composeFilter } from '@/sheep/utils';
  import useClip from '@/sheep/utils/clipboard.js';
  import GoodsEdit from '@/app/shop/admin/goods/goods/edit.vue';
  import CommentEdit from './edit.vue';
  import CommentRecyclebin from './recyclebin.vue';
  import CommentFakeComment from './fakeComment.vue';
  import { cloneDeep } from 'lodash';

  const props = defineProps(['modal']);

  // 列表
  const table = reactive({
    data: [],
    order: '',
    sort: '',
    selected: [],
  });
  const { pageData } = usePagination();
  const loading = ref(true);
  const filterParams = reactive({
    tools: {
      'goods.title': {
        type: 'tinput',
        label: '商品名称',
        field: 'goods.title',
        value: '',
      },
      user: {
        type: 'tinputprepend',
        label: '用户信息',
        field: 'user',
        placeholder: '请输入查询内容',
        user: {
          field: 'user.nickname',
          value: '',
        },
        options: [
          {
            label: '用户昵称',
            value: 'user.nickname',
          },
          {
            label: '用户手机号',
            value: 'user.mobile',
          },
        ],
      },
      content: {
        type: 'tinput',
        label: '评价内容',
        field: 'content',
        value: '',
      },
      status: {
        type: 'tselect',
        label: '状态',
        field: 'status',
        value: '',
        options: {
          data: [
            {
              label: '显示',
              value: 'normal',
            },
            {
              label: '隐藏',
              value: 'hidden',
            },
          ],
        },
      },
    },
    data: {
      'goods.title': '',
      user: { field: 'user.nickname', value: '' },
      content: '',
      status: '',
    },
    conditionLabel: {},
  });
  const { openFilter, deleteFilter } = useSearch({ filterParams, getData });
  // 获取
  async function getData(page) {
    loading.value = true;
    if (page) pageData.page = page;
    let tempSearch = cloneDeep(filterParams.data);

    if (props.modal) {
      tempSearch = { tempSearch, ...props.modal.params.data };
    }

    let search = composeFilter(tempSearch, {
      'goods.title': 'like',
      'user.nickname': 'like',
      'user.mobile': 'like',
      content: 'like',
    });
    const { error, data } = await api.comment.list({
      page: pageData.page,
      list_rows: pageData.list_rows,
      order: table.order,
      sort: table.sort,
      ...search,
    });
    if (error === 0) {
      table.data = data.data;
      pageData.page = data.current_page;
      pageData.list_rows = data.per_page;
      pageData.total = data.total;
    }
    loading.value = false;
  }
  //商品详情
  function openGoods(e) {
    useModal(GoodsEdit, {
      title: '商品',
      type: 'edit',
      id: e,
    });
  }

  // table 字段排序
  function fieldFilter({ prop, order }) {
    table.order = order == 'ascending' ? 'asc' : 'desc';
    table.sort = prop;
    getData();
  }
  //table批量选择
  function changeSelection(row) {
    table.selected = row;
  }
  const batchHandleTools = [
    {
      type: 'delete',
      label: '删除',
      auth: 'shop.admin.goods.comment.delete',
      class: 'danger',
    },
    {
      type: 'normal',
      label: '正常',
      auth: 'shop.admin.goods.comment.edit',
      class: 'success',
    },
    {
      type: 'hidden',
      label: '隐藏',
      auth: 'shop.admin.goods.comment.edit',
      class: 'info',
    },
  ];
  async function handleCommand(e) {
    await api.comment.edit(e.id, {
      status: e.type,
    });
    getData();
  }
  function editRow(row) {
    useModal(
      CommentEdit,
      {
        title: '评价详情',
        type: 'edit',
        id: row.id,
      },
      {
        confirm: () => {
          getData();
        },
      },
    );
  }

  function openRecyclebin() {
    useModal(
      CommentRecyclebin,
      {
        title: '回收站',
      },
      {
        confirm: () => {
          getData();
        },
      },
    );
  }
  //虚拟评价
  function fakeCommentRow() {
    useModal(
      CommentFakeComment,
      { title: '添加' },
      {
        confirm: () => {
          getData();
        },
      },
    );
  }

  // 删除api 单独批量可以直接调用
  async function deleteApi(id) {
    await api.comment.delete(id);
    getData();
  }
  async function batchHandle(type) {
    let ids = [];
    table.selected.forEach((row) => {
      ids.push(row.id);
    });
    switch (type) {
      case 'delete':
        ElMessageBox.confirm('此操作将删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          deleteApi(ids.join(','));
        });
        break;
      default:
        await api.comment.edit(ids.join(','), {
          status: type,
        });
        getData();
    }
  }

  onMounted(() => {
    getData();
  });
</script>
<style lang="scss" scoped>
  .copy {
    color: var(--el-color-primary);
  }
  .status-hidden {
    color: #999999;
  }
  .status-normal {
    color: #52c41a;
  }
  .comment-view {
    .el-header {
      height: auto;
    }

    .el-main {
      .sa-table-wrap {
        height: 100%;
      }
    }
    .goods-title {
      color: var(--el-color-primary);
    }
    .status-btn {
      height: 24px;
      font-size: 12px;
      color: #999999;
      background: rgba(153, 153, 153, 0.16);
      width: 56px;
      border-radius: 4px;
      border: none;
    }
    .success-btn {
      color: #52c41a;
      background: rgba(82, 196, 26, 0.16);
    }
  }
  :deep() {
    .el-rate .el-rate__icon.is-active {
      color: #faad14;
    }
    .el-rate.is-disabled .el-rate__item {
      color: var(--sa-place);
      height: 24px;
      .el-icon svg {
        width: 24px;
        height: 24px;
      }
    }
    .el-table__body-wrapper {
      // font-size: 12px;
    }
    .user-avatar {
      .el-image {
        border-radius: 12px !important;
      }
    }
    .images-wrap {
      width: 176px;
    }
  }
</style>
